/*
 * TransactionStatus.java    ver   22/ott/2009
 *
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transactions;

/**
 * Enumerates the possible states for a Transaction to be in.
 * <p>
 * See the documentation for the single values for more accurate descriptions
 * </p>
 * 
 * @version 0.0.1 22/ott/2009
 * @author Andrea Reale <andrea.reale@gmail.com>
 * 
 */
public enum TransactionStatus {
    /**
     * The transaction was asked to start but still doesn't have a valid
     * identifier assigned.
     */
    BEGINNING,
    /**
     * The transaction is currently active and running. Modification to
     * TransactionalObjects in the context of this transaction are not performed
     * on the actual objects, but on tentative snapshots. The modification will
     * be applied to the objects at commit time
     */
    RUNNING,
    /**
     * The transaction was asked to commit and it is now being validated by the
     * local Weak Replica Manager, against other concurrent local transactions.
     */
    LOCAL_VALIDATION,
    /**
     * The transaction has passed the LOCAL VALIDATION and is currently in the
     * GLOBAL VALIDATION phase, which means it is being validated by the Replica
     * Managers, and it is waiting for a final outcome.
     */
    GLOBAL_VALIDATION,
    /**
     * The transaction is no longer active in the system. It was asked to commit
     * and the commit procedure was succesful. Therefore its effects were
     * applied on the actual reources involved in the transaction.
     */
    COMMITTED,
    /**
     * The transaction is no longer active. Either this transaction was rolled
     * back at commit time due to conflicts with other concurrent transactions
     * or the user explicitely asked to roll back it.
     */
    ROLLED_BACK,
    /**
     * Returned when no transaction is associated to the current thread
     */
    NO_TRANSACTION;

    /**
     * Returns <code>false</code> iff the status is one of
     * {@link #NO_TRANSACTION}, {@link #COMMITTED}, {@link #ROLLED_BACK}.
     * 
     * @return <code>false</code> iff the status is one of
     *         {@link #NO_TRANSACTION}, {@link #COMMITTED}, {@link #ROLLED_BACK}
     *         .
     */
    public boolean isActive() {
	final boolean notActive = this.equals(TransactionStatus.NO_TRANSACTION)
		|| this.equals(TransactionStatus.COMMITTED)
		|| this.equals(TransactionStatus.ROLLED_BACK);
	return !notActive;
    }
}
